/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gui.servidor;

import br.gui.cliente.Hospede;
import br.gui.cliente.Hotel;
import br.gui.cliente.Reserva;
import br.gui.dao.Container;
import br.gui.dao.DAOHospede;
import br.gui.dao.DAOHotel;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JOptionPane;

/**
 *
 * @author Anderson
 */
public class Servidor extends UnicastRemoteObject implements IServidor {
    
    private Reserva reserva;
    private Hospede hospede;
    private Hotel hotel;
    private DAOHospede daoHospede;
    private DAOHotel daoHotel;
   

    public Servidor() throws RemoteException {
        File banco = new File("banco.hilton");
        if (banco.exists()) {
            Container.openFile();
        } else {
            Container.saveFile();
        }
    }
    // ------------ HOSPEDES -----------------//

    
    public void criarHospede(Hospede hospede) throws RemoteException, IOException{
       System.out.print(hospede.getNome());
       // if (daoHospede.findByCpf(hospede.getCPF())== null){
           
        hospede.getCPF();
        hospede.getNome();
        hospede.getEndereco();
        hospede.getEmail();
        hospede.getTelefone();
      
        
        daoHospede.persist(hospede);
        daoHospede.commit();
        
//        }else
//            JOptionPane.showMessageDialog(null, "Cliente já existe...");
//        Hospede h = new Hospede(cpf, nome, endereco, email, telefone);
        
//        new DAOHospede().persist(hospede);
//        new DAOHospede().commit();
}

    public void excluirHospede(String cpf) throws RemoteException, IOException {
        DAOHospede dh = new DAOHospede();
        Hospede h = dh.findByCpf(cpf);
        dh.remove(h);
        dh.commit();
    }

    public void atualizarHospede(Hospede hospede) throws RemoteException, IOException {
        if(daoHospede.findByCpf(hospede.getCPF())!=null){
            hospede.getEmail();
            hospede.getEndereco();
            hospede.getNome();
            hospede.getTelefone();
            
            daoHospede.merge(hospede);
            daoHospede.commit();
        }
        else{
            JOptionPane.showMessageDialog(null, "Hospede não existe...");
        }
    
    }
   

    // ------------ HOTEL -----------------//  
    public Hotel criarHotel(String nome, String gerente, Integer numeroDeQuartos,String endereco) throws RemoteException, IOException {
        Hotel hotel = new Hotel();
        hotel.setNome(nome);
        hotel.setNomeGerente(gerente);
        hotel.criarQuartos(numeroDeQuartos);
        hotel.setEndereco(endereco);

        DAOHotel daoHotel = new DAOHotel();
        daoHotel.persist(hotel);
        daoHotel.commit();

        return hotel;
    }

    public void excluirHotel(String nome) throws RemoteException, IOException {
        DAOHotel dho = new DAOHotel();
        Hotel h = dho.findByNome(nome);
        dho.remove(h);
        dho.commit();
    }
    
    public void atualizarHotel(Hotel hotel) throws RemoteException, IOException{
     if(daoHotel.findByNome(hotel.getNome())!=null){
            hotel.getEndereco();
            hotel.getNomeGerente();
            
            daoHospede.merge(hotel);
            daoHospede.commit();
        }
        else{
            JOptionPane.showMessageDialog(null, "Hospede não existe...");
        }
    }
    // ------------ RESERVA----------------//  
      
  /*     public Reserva addReserva(String cpf, String nome, int numeroDoQuarto,Calendar dataInicio,Calendar dataFim  ) throws RemoteException, IOException {
       
        if(DAOHospede.findByCPF(cpf)!=null)
        reserva.sethospede(hospede.setCPF(cpf));
        hotel.setNomeGerente(gerente);
        hotel.criarQuartos(numeroDeQuartos);

        DAOHotel daoHotel = new DAOHotel();
        daoHotel.persist(hotel);
        daoHotel.commit();

        return hotel;
    }*/
}
